package com.vipheyue.livegamecontrol;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.vipheyue.livegamecontrol.bean.GameBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.btn_getLatest)
    Button btn_getLatest;
    @Bind(R.id.et_result)
    EditText et_result;
    @Bind(R.id.btn_setResult)
    Button btn_setResult;
    @Bind(R.id.btn_newGame)
    Button btn_newGame;
    GameBean currentGameBean = null;
    Boolean havedLatesBean = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_getLatest, R.id.btn_setResult, R.id.btn_newGame, R.id.btn_stopBet,R.id.btn_charge})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_getLatest:
                getLatesMethod();
                break;
            case R.id.btn_setResult:
                setResultMethod();
                break;
            case R.id.btn_newGame:
                createNewGame();
                break;
            case R.id.btn_stopBet:
                stopBet();
                break;
            case R.id.btn_charge:
                charge();
                break;
        }
    }

    private void charge() {
        startActivity(new Intent(this,RechargeActivity.class));
    }

    private void stopBet() {


        BmobQuery<GameBean> query = new BmobQuery<GameBean>();
        query.setLimit(1); // 限制最多10条数据结果作为一页
        query.order("-updatedAt");
        query.findObjects(this, new FindListener<GameBean>() {
            @Override
            public void onSuccess(List<GameBean> object) {
                currentGameBean = object.get(0);
                havedLatesBean = true;
                currentGameBean. setState(2);//进入到等待开奖state
                currentGameBean.update(MainActivity.this, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(MainActivity.this, "停止下注成功", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(MainActivity.this, "停止下注失败,请重新操作: " + i + s, Toast.LENGTH_SHORT).show();
                    }
                });
               }

            @Override
            public void onError(int code, String msg) {
                Toast.makeText(MainActivity.this, "获取最新数据错误码: " + code + msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void createNewGame() {
        GameBean bean = new GameBean();
        bean.setFinish(false);
        bean.setTotalIn_dong(0);
        bean.setTotalIn_nan(0);
        bean.setTotalIn_xi(0);
        bean.setTotalIn_bei(0);
        bean.setState(1);//可以下注
        bean.save(this, new SaveListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(MainActivity.this, "重新开启一局Game成功", Toast.LENGTH_SHORT).show();
                getLatesMethod();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(MainActivity.this, "重新开启一局Game失败: " + i + s, Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void getLatesMethod() {
        BmobQuery<GameBean> query = new BmobQuery<GameBean>();
        query.setLimit(1); // 限制最多10条数据结果作为一页
        query.order("-updatedAt");
        query.findObjects(this, new FindListener<GameBean>() {
            @Override
            public void onSuccess(List<GameBean> object) {
                currentGameBean = object.get(0);
                havedLatesBean = true;
                Toast.makeText(MainActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
                Log.d("TestActivity", "currentGameBean.getTotalIn_dong():" + currentGameBean.getTotalIn_dong());
            }

            @Override
            public void onError(int code, String msg) {
                Toast.makeText(MainActivity.this, "获取最新数据错误码: " + code + msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setResultMethod() {
        int answer = 0;
        try {
            answer = Integer.parseInt(et_result.getText().toString().trim());
        } catch (NumberFormatException e) {
            Toast.makeText(this, "输入错误,请输入1-4", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            currentGameBean.setFinish(true);
        } catch (Exception e) {//currentGameBean为空
            getLatesMethod();
            Toast.makeText(this, "请再次点击获取当前最新游戏", Toast.LENGTH_SHORT).show();
            return;
        }

        switch (answer) {
            case 1:
                currentGameBean.setAnswer(1);
                break;
            case 2:
                currentGameBean.setAnswer(2);
                break;
            case 3:
                currentGameBean.setAnswer(3);
                break;
            case 4:
                currentGameBean.setAnswer(4);
                break;
            default:
                Toast.makeText(this, "输入错误,请输入1-4", Toast.LENGTH_SHORT).show();
                return;
        }
        currentGameBean.setState(3);//进入到开奖成功state
        currentGameBean.update(this, new UpdateListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(MainActivity.this, "开奖成功", Toast.LENGTH_SHORT).show();
                currentGameBean = null;
//                createNewGame();

            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(MainActivity.this, "开奖失败: " + i + s, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
